package com.app.biz.controller;

import com.app.biz.domain.BizTopHundredIndustryEnterprise;
import com.app.biz.service.IBizTopHundredIndustryEnterpriseService;
import com.app.common.annotation.Log;
import com.app.common.config.AppConfig;
import com.app.common.core.controller.BaseController;
import com.app.common.core.domain.AjaxResult;
import com.app.common.core.page.TableDataInfo;
import com.app.common.enums.BusinessType;
import com.app.common.utils.file.FileUploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/biz/hundred/industry/enterprise")
public class BizTopHundredIndustryEnterpriseController extends BaseController
{
    @Autowired
    private IBizTopHundredIndustryEnterpriseService service;




    @GetMapping("/list")
    public TableDataInfo list(BizTopHundredIndustryEnterprise bizTopHundredIndustryEnterprise)
    {
        startPage();
        List<BizTopHundredIndustryEnterprise> list = service.selectBizTopHundredIndustryEnterpriseList(bizTopHundredIndustryEnterprise);
        return getDataTable(list);
    }





    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(service.selectBizTopHundredIndustryEnterpriseByBaseId(baseId));
    }



    @Log(title = "山东省工业百强企业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizTopHundredIndustryEnterprise bizTopHundredIndustryEnterprise)
    {
        return toAjax(service.insertBizTopHundredIndustryEnterprise(bizTopHundredIndustryEnterprise));
    }


    @Log(title = "山东省工业百强企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizTopHundredIndustryEnterprise bizTopHundredIndustryEnterprise)
    {
        return toAjax(service.updateBizTopHundredIndustryEnterprise(bizTopHundredIndustryEnterprise));
    }


    @Log(title = "山东省工业百强企业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(service.deleteBizTopHundredIndustryEnterpriseByBaseIds(baseIds));
    }

    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam Map<String,String> params, @RequestPart("file") MultipartFile multipartFile){
        try{
            String year = params.get("year");
            if(StringUtils.isBlank(year)){
                return AjaxResult.warn("请选择导入的年份");
            }

            /* 上传文件的路径 */
            String uploadPath = FileUploadUtils.upload(AppConfig.getProfile(),multipartFile);
            String absoluteExcelPath = FileUploadUtils.replaceAbsolutePath(uploadPath);
            AjaxResult ajaxResult = service.importExcel(absoluteExcelPath, year);
            return ajaxResult;
        }catch (IOException e){
            return AjaxResult.error("导入失败");
        }
    }


}
